import TableLyout from '@/components/commonPublic/tableLyoutSpecification.vue'
import tree from "@/components/materialDataManage/tree"; //左侧机构树
import {
    getSampleTrapNodeList, //树状图
} from "@/api/request.js";
import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置
import FormLyout from '@/components/commonPublic/fromLayout.vue'

export default {
    name: "testCategory",
    data() {
        return {
            name: "testCategory",
            defaultProps: {
                children: "children",
                label: "fNodename",
            },
            treeText: {
                companyId: null,
                userId: null,
            },
            formObj:formJs.formObj,
            formData:{},
            companyTree: "", //所有数据
            treeData: [], //左侧树状图数据
            // 机构树
            highlightBd: true, //左侧机构树停留背景色
            treelength: false, //左侧树显示隐藏
            // checked: true, //查看下级按钮
            showName: "未选择样品",
            checkData: [], //树状图默认选中id


            isLock: false,
            projectId: null,
            projectName: null,
            loading: false,
            btnObj: [], //按钮
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
        
          
            isShow: false, //是否显示弹框

            statusNum: null, //点击那个按钮
            dataFid: null, //列表详情id
            currentRow: null, //列表详情
            activeIndex: "1",
            fId: null, //当前数据fId
            tableData: [],
            companyId: null,
            currentRowId: "",
            dialogTitle: null,
        };
    },
    components: {
        TableLyout,
        publicForm,
        tree,
        FormLyout
    },
    mounted() {
        this.getTree();
    },
    methods: {


        closeDialog() {
            this.isShow = false;
        },

        // 左侧树结构
        getTree() {
            getSampleTrapNodeList()
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.sampleTrapNodeList;
                        this.treeData = this.tranListToTreeData(companyTree);
                        if (this.treeData[0].children == undefined) {
                            this.treelength = false; //左侧组织机构是否显示
                        } else {
                            this.treelength = true;
                            this.formData=this.treeData[0];
                            this.checkData[0]=this.treeData[0].fId;
                        }
                        this.showName = this.treeData[0].fNodename; //当前点击组织机构名称
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        tranListToTreeData(data) {
            let result = [];
            if (!Array.isArray(data)) {
              return result;
            }
            // 清除已存在的children 重新赋值
            data.forEach((item) => {
              delete item.children;
            });
      
            let map = {};
            data.forEach((item) => {
              map[item.fId] = item;
            });
      
            data.forEach((item) => {
              let parent = map[item.fParentid];
              if (parent) {
                (parent.children || (parent.children = [])).push(item);
              } else {
                result.push(item);
              }
            });
            return result;
          },
        // 点击左侧机构树
        handleNodeClick(data) {
            this.showName = data.fNodename; //顶部显示机构名
            this.formData=data
        },
        handleRow() {},

        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val
            this.dataFid = val.fId;
        },
  
        
 
    },
};